package com.tanhua.manage.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.api.OptionsApi;
import com.tanhua.dubbo.api.QuestionnaireApi;
import com.tanhua.model.db.Option;
import com.tanhua.model.db.QQuestion;
import com.tanhua.model.db.Questionnaire;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SoulService {

    @DubboReference
    private QuestionnaireApi questionnaireApi;

    @DubboReference
    private OptionsApi optionsApi;

    // 查询问卷列表
    public PageResult findList(Integer pageNumber, Integer pageSize) {

        // 分页查询
        Page<Questionnaire> iPage = questionnaireApi.selectAll(pageNumber, pageSize);

        // 获取问卷数量
        long total = iPage.getTotal();

        // 获取问卷列表
        List<Questionnaire> questionnaireList = iPage.getRecords();

        // 判断
        if (questionnaireList == null || questionnaireList.size() <= 0){

            return new PageResult();
        }

        return new PageResult(pageNumber,pageSize,total,questionnaireList);
    }

    // 查询问题列表
    public PageResult selectQuestions(Integer pageNumber, Integer pageSize, String level) {


        // 分页查询
        Page<QQuestion> iPage = questionnaireApi.selectQuestions(pageNumber,pageSize,level);

        // 获取问题列表
        long total = iPage.getTotal();

        // 获取问题列表
        List<QQuestion> questions = iPage.getRecords();

        List<QQuestion> questionList = new ArrayList<>();

        // 循环遍历
        for (QQuestion question : questions) {

            // 获取问题的id
            Integer questionId = question.getId();

            // 查询问题对应的选项
            List<Option> optionList = optionsApi.selectOptions(questionId);

            // 转换为数组
            Object[] options = optionList.toArray();

            // 封装数据
            question.setOptions(options);

            questionList.add(question);
        }

        return new PageResult(pageNumber,pageSize,total,questionList);
    }

    // 保存问题
    public void saveQuestions(QQuestion questions) {

        // 保存问题内容
        Integer questionsId = questionnaireApi.saveQuestions(questions);

        // System.out.println(questionsId);

        // 获取选项
        Object[] options = questions.getOptions();

        // 保存选项
        for (Object option : options) {

            // 构造数据
            Option option1 = new Option();
            option1.setContent(String.valueOf(option));
            option1.setQuestionId(questionsId);

            // 保存选项
            optionsApi.saveOption(option1);
        }
    }

    // 删除问题
    public void deleteQuestions(Integer questionId) {

        // 删除问题对应选项
        optionsApi.deleteByQuestionId(questionId);

        // 删除问题
        questionnaireApi.deleteQuestion(questionId);

    }

    // 根据id查询问题
    public QQuestion selectQuestionById(Integer questionId) {

        // 查询问题
        QQuestion question = questionnaireApi.selectQuestionById(questionId);

        // 查询选项
        List<Option> optionList = optionsApi.selectOptions(questionId);

        List<String> content = CollUtil.getFieldValues(optionList, "content", String.class);

        // 转换为数组
        Object[] options = content.toArray();

        // 封装数据
        question.setOptions(options);

        return question;
    }

    // 更新问题
    public void updateQuestions(QQuestion questions) {


        // 更新问题内容
        questionnaireApi.updateQuestions(questions);

        // 获取问题id
        Integer questionId = questions.getId();

        // 获取选项内容
        Object[] options = questions.getOptions();

        // 清空id对应的选项
        optionsApi.deleteByQuestionId(questionId);

        // 保存选项
        for (Object option : options) {

            // 构造数据
            Option option1 = new Option();
            option1.setContent(String.valueOf(option));
            option1.setQuestionId(questionId);

            // 保存选项
            optionsApi.saveOption(option1);
        }
    }
}
